//
//  SettingBasic.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/9/27.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation



class SettingBasic: NNView {
    
    var canSelected:Bool = false {
        didSet{
            self.bgView.isEnable = canSelected
        }
    }
    
    var isSelected:Bool = false {
        didSet{
            self.setStyle(isSelected: isSelected)
        }
    }
    
    var mouseDown:((SettingBasic)->())?
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        
    }
    
    convenience init(mouseDwon:((SettingBasic)->())?) {
        self.init()
        self.mouseDown = mouseDwon
        self.setupUI()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        self.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        bgView.addSubview(title)
        title.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.height.equalTo(18)
        }
    }
    
    lazy var bgView: MSView = {
        let view = MSView.init()
        view.wantsLayer = true
        view.tap = { current in
            if let mouseDown = self.mouseDown {
                mouseDown(self)
            }
        }
        view.status = { mouseStatus in
            self.setStatus(view: view, status: mouseStatus)
        }
        return view
    }()
    
    func setStatus(view:MSView,status:OneStatus) {
        switch status {
        case .normal,.selected:
            self.bgView.layer?.borderColor = NSColor.clear.cgColor
            self.bgView.layer?.borderWidth = 0
        case.highlight,.hover:
            self.bgView.layer?.borderColor = color_main140.cgColor
            self.bgView.layer?.borderWidth = 1
        }
    }
    
    lazy var title: NSLabel = {
        let lab = NSLabel.label(title: "...", textColor: color_gray(0), font: NSFont.systemFont(ofSize: 13))
        lab.alignment = .center
        lab.sizeToFit()
        return lab
    }()
    
    
    func setStyle(isSelected:Bool) {
        self.bgView.wantsLayer = true
        self.title.wantsLayer = true
        if isSelected {
            self.bgView.layer?.cornerRadius = 6
            self.bgView.layer?.backgroundColor = color_main140.cgColor
            self.title.textColor = color_gray(255)
        }else {
            self.bgView.layer?.cornerRadius = 6
            self.bgView.layer?.backgroundColor = color_gray(220).cgColor
            self.title.textColor = color_gray(0)
        }
    }
    
}
